"""
Copyright (c) 2021 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : GUC
Case Name   : 使用alter user方法设置参数TimeZone为Australia/South,观察预期结果
Description :
        1.查询TimeZone默认值
        2.创建用户
        3.修改参数值为Australia/South并查询当前时间
        4.删除用户
Expect      :
        1.显示默认值PRC
        2.用户创建成功
        3.设置成功当前时间为北京时间
        4.删除成功
History     :
"""
import time
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ClientConnection(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---Opengauss_Function_Guc_ClientConnection_Case0133start------')
        self.Constant = Constant()
        self.commonsh = CommonSH('dbuser')
        self.userNode = Node('dbuser')

    def test_TimeZone(self):
        self.log.info('查询默认值')
        sql_cmd = self.commonsh.execut_db_sql(f'''show TimeZone;''')
        self.log.info(sql_cmd)
        self.res = sql_cmd.splitlines()[-2].strip()
        self.log.info('创建测试用户')
        sql_cmd = self.commonsh.execut_db_sql(f'''drop user if exists 
            test_spur0133 cascade;
            create user test_spur0133 password '{macro.COMMON_PASSWD}';''')
        self.log.info(sql_cmd)
        self.assertIn(self.Constant.CREATE_ROLE_SUCCESS_MSG, sql_cmd)
        self.log.info('修改用户级别参数')
        sql_cmd = self.commonsh.execut_db_sql('''alter user test_spur0133 
            set TimeZone to 'Australia/South';''')
        self.log.info(sql_cmd)
        self.assertIn(self.Constant.ALTER_ROLE_SUCCESS_MSG, sql_cmd)
        time.sleep(3)
        self.log.info('查询时间')
        sql_cmd = '''show TimeZone;
                      select now();'''
        excute_cmd = f'''
                        source {macro.DB_ENV_PATH};
                        gsql -d {self.userNode.db_name} \
                        -p {self.userNode.db_port} \
                        -U test_spur0133 \
                        -W '{macro.COMMON_PASSWD}' \
                        -c "{sql_cmd}"
                             '''
        self.log.info(sql_cmd)
        msg1 = self.userNode.sh(excute_cmd).result()
        self.log.info(msg1)
        self.assertIn('Australia/South', msg1)
        self.assertIn('+09:30', msg1)

    def tearDown(self):
        self.log.info('----------------恢复默认值-----------------------')
        sql_cmd = self.commonsh.execut_db_sql('''drop user if exists 
            test_spur0133 cascade;''')
        self.log.info(sql_cmd)
        self.log.info(
            '----Opengauss_Function_Guc_ClientConnection_Case0133执行完成---')
